/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wikitrafic.iovanalex.ro;



/**
 *
 * @author Administrator
 */
public class GPSPoint {

    private double lat;
    private double lon;
    private double alt = 0;
    private double DEG_TO_RAD = 0.017453292519943295769236907684886;
    private double EARTH_RADIUS_IN_METERS = 6372797.560856;

    public GPSPoint(double lat, double lon) {
        this.lat = lat;
        this.lon = lon;
    }

    public GPSPoint(double lat, double lon, double alt) {
        this.lat = lat;
        this.lon = lon;
        this.alt = alt;
    }

    public double getLat() {
        return lat;
    }

    public double getLon() {
        return lon;
    }

    public double getAlt() {
        return alt;
    }
    
    //uses WGS-84
    // from http://blog.julien.cayzac.name/2008/10/arc-and-distance-between-two-points-on.html
    double getDistanceInMeters(GPSPoint from, GPSPoint to) {
        return EARTH_RADIUS_IN_METERS * arcInRadians(from, to);
    }

    double getDistanceInMeters(GPSPoint to){
        return getDistanceInMeters(this, to);
    }

    double arcInRadians(GPSPoint from, GPSPoint to) {
        double latitudeArc = (from.getLat() - to.getLat()) * DEG_TO_RAD;
        double longitudeArc = (from.getLon() - to.getLon()) * DEG_TO_RAD;

        double latitudeH = Math.sin(latitudeArc * 0.5);
        latitudeH *= latitudeH;
        
        double lontitideH = Math.sin((longitudeArc * 0.5));
        lontitideH*=lontitideH;

        double tmp = Math.cos(from.getLat() * DEG_TO_RAD) * Math.cos(to.getLat() * DEG_TO_RAD);
        return 2.0 * Math.asin(Math.sqrt(latitudeH + tmp * lontitideH));
    }

    
    }